<!-- ========================================= CONTENT ========================================= -->
<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use frontend\models\Address;

$cartTotal = $this->params['cartTotal'];
$cartList = $this->params['cartList'];
?>
<section id="checkout-page">
	<div class="container">
		<div class="col-xs-12 no-margin">
			<button class="btn le-button btn-lg" onclick="showAddressForm()">新建地址信息</button>
			<button class="btn le-button btn-lg" onclick="hideAddressForm()">收起地址表单</button>
			<div class="billing-address" style="display:none" id="billing-address">
				<h2 class="border h1">填写收货地址</h2>
				<?php
				$model = new Address();
				$form = ActiveForm::begin(['fieldConfig' => [//统一修改字段的模板
							],]);
				?>
				<div class="row field-row">
					<?=
							$form->field($model, 'firstname', [
								'options' => ['class' => "col-xs-12 col-sm-6"],
								'inputOptions' => ['class' => 'le-input'],
							])
							->textInput(['maxlength' => true,])
					?>
					<?=
					$form->field($model, 'lastname', [
						'options' => ['class' => "col-xs-12 col-sm-6"],
						'inputOptions' => ['class' => 'le-input'],
					])->textInput(['maxlength' => true])
					?>
				</div><!-- /.field-row -->
				<div class="row field-row">
					<?=
					$form->field($model, 'company', [
						'options' => ['class' => "col-xs-12"],
						'inputOptions' => ['class' => 'le-input'],
					])->textInput(['maxlength' => true])
					?>
				</div><!-- /.field-row -->
				<div class="row field-row">
					<?=
					$form->field($model, 'address', [
						'options' => ['class' => "col-xs-12 col-sm-6"],
						'inputOptions' => ['class' => 'le-input'],
					])->textInput()
					?>

					<?=
					$form->field($model, 'postcode', [
						'options' => ['class' => "col-xs-12 col-sm-6"],
						'inputOptions' => ['class' => 'le-input'],
					])->textInput(['maxlength' => true])
					?>
				</div><!-- /.field-row -->
				<div class="row field-row">
					<?=
					$form->field($model, 'email', [
						'options' => ['class' => "col-xs-12 col-sm-6"],
						'inputOptions' => ['class' => 'le-input'],
					])->textInput(['maxlength' => true])
					?>

					<?=
					$form->field($model, 'telephone', [
						'options' => ['class' => "col-xs-12 col-sm-6"],
						'inputOptions' => ['class' => 'le-input'],
					])->textInput(['maxlength' => true])
					?>
				</div>
				<div class="">
					<div class="col-md-10"></div>
					<div class="form-group">
						<?= Html::submitButton($model->isNewRecord ? '添加' : '更新', ['class' => $model->isNewRecord ? 'btn le-button btn-lg' : 'btn btn-primary', 'style' => 'float: right']) ?>
					</div>

					<?php ActiveForm::end(); ?>
				</div>
			</div>

			<section id="shipping-address">
				<h2 class="border h1">选择地址</h2>
				<form >
					<?php foreach ($addressList as $addressUnit) {
						?>
						<div class="row field-row">
							<div class="col-xs-12">
								<input  onclick="setAddressId(<?php echo $addressUnit['addressId']; ?>)" class="le-radio big" 
										type="radio"  name="address" value="<?php echo $addressUnit['addressId']; ?>"/>
								<a class="simple-link bold" href="#">
									<?php echo $addressUnit['firstname']; ?>
									<?php echo $addressUnit['lastname']; ?> 
									<?php echo $addressUnit['address']; ?> 
									<?php echo $addressUnit['telephone']; ?></a>
							</div>
						</div><!-- /.field-row -->
					<?php } ?>
				</form>
			</section><!-- /#shipping-address -->


			<section id="your-order">
				<h2 class="border h1">你的订单</h2>
				<form id="orderList" method="post" action="<?php echo yii\helpers\Url::toRoute('shop-order/create'); ?>">
					<input id = "addressId" type="hidden" name="address"value="<?php echo isset($addressList['0']['addressId']) ? isset($addressList['0']['addressId']) : 0; ?>">
					<?php
					$cartTotal = 0;
					foreach ($cartIdList as $value) {
						$cartUnit = $cartList[$value];
						$cartTotal += $cartUnit['productnum'] * $cartUnit['price']
						?>
						<input name="_csrf" type="hidden" id="_csrf" value="<?= Yii::$app->request->csrfToken ?>">
						<input type="hidden" name="cartIdS[]"value="<?php echo $cartUnit['cartId'] ?>">
						<div class="row no-margin order-item">
							<div class="col-xs-12 col-sm-1 no-margin">
								<a href="#" class="qty"><?php echo $cartUnit['productnum'] ?> x</a>
							</div>
							<div class="col-xs-12 col-sm-1 no-margin">
								<a href="#" class="thumb-holder">
									<img class="lazy" alt="" src="//<?php echo $cartUnit['productCover'] ?>" />
								</a>
							</div>
							<div class="col-xs-12 col-sm-1 no-margin">

							</div>
							<div class="col-xs-12 col-sm-6 ">
								<div class="title"><a href="#"><?php echo $cartUnit['productTitle'] ?> </a></div>
								<div class="brand">热卖</div>
							</div>

							<div class="col-xs-12 col-sm-2 no-margin">
								<div class="price"><?php echo $cartUnit['productTotal'] ?></div>
							</div>
						</div><!-- /.order-item -->
					<?php } ?>
				</form>
			</section><!-- /#your-order -->

			<div id="total-area" class="row no-margin">
				<div class="col-xs-12 col-lg-4 col-lg-offset-8 no-margin-right">
					<div id="subtotal-holder">
						<ul class="tabled-data inverse-bold no-border">
							<li>
								<label>订单总价</label>
								<div class="value "><?php echo $cartTotal ?></div>
							</li>
							<li>
								<label>运费选择</label>
								<div class="value">
									<div class="radio-group">
										<input class="le-radio" type="radio" name="group1" value="free"> <div class="radio-label bold">包邮</div><br>
<!--											<input class="le-radio" type="radio" name="group1" value="local" checked>  <div class="radio-label">local delivery<br><span class="bold">$15</span></div>-->
									</div>
								</div>
							</li>
						</ul><!-- /.tabled-data -->

						<ul id="total-field" class="tabled-data inverse-bold ">
							<li>
								<label>订单总价</label>
								<div class="value"><?php echo $cartTotal ?></div>
							</li>
						</ul><!-- /.tabled-data -->

					</div><!-- /#subtotal-holder -->
				</div><!-- /.col -->
			</div><!-- /#total-area -->

			<div id="payment-method-options">
				<form >
					<div class="payment-method-option">
						<input class="le-radio" type="radio" name="group2" value="paypal">
						<div class="radio-label bold ">支付宝支付</div>
					</div><!-- /.payment-method-option -->
				</form>
			</div><!-- /#payment-method-options -->

			<div class="place-order-button">
				<div class="">
					<div class="form-group">
						<?= Html::submitButton($model->isNewRecord ? '提交订单' : '更新', ['class' => $model->isNewRecord ? 'btn le-button big' : 'btn btn-primary big', 'style' => 'float: right', 'onclick' => 'createOrder()']) ?>
					</div>

				</div>
				<!--<button name="submit" class="le-button big">提交订单</button>-->
			</div><!-- /.place-order-button -->

		</div><!-- /.col -->
	</div><!-- /.container -->    
</section><!-- /#checkout-page -->

<!-- ========================================= CONTENT : END ========================================= -->	
<script>
	function showAddressForm()
	{
		var addressForm = document.getElementById('billing-address');
		addressForm.style.display = 'block';
	}
	function hideAddressForm()
	{
		var addressForm = document.getElementById('billing-address');
		addressForm.style.display = 'none';
	}
	function createOrder() {
//		alert('createOrder');
		var iForm = document.getElementById('orderList');
		var input = document.getElementById('addressId').value;
		if (input == 0) {
			alert("请先新建地址");
		} else
		{
			iForm.submit();
		}
	}
	function setAddressId(addressId)
	{
		document.getElementById('addressId').value = addressId;
	}
</script>